package File;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author 王艺锡
 * @version 1.0
 * 文件的创建
 */
public class FileCreate {
    public static void main(String[] args) {

    }

    @Test
    //方式1:new File(String pathname)通过文件路径创建File对象
    public void create01(){
        String filepath = "e:\\new1.txt";
        File file = new File(filepath);

        try {
            //只有执行createNewFile方法，才会真正的在磁盘中创建一个文件
            file.createNewFile();
            System.out.println("new1文件创建成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    //方式2:new File(File parent,String child)//根据父目录文件+子文件名称来构建
    public void create02(){
        File parentFile = new File("e:\\");
        String fileName = "new2.txt";
        File file = new File(parentFile, fileName);

        try {
            file.createNewFile();
            System.out.println("new2创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    //方法3:new File(String parent,String child)//根据父目录+子路径来构建
    public void create03(){
    String parentPath = "e:\\";
    String fileName = "new3.txt";
        File file = new File(parentPath, fileName);

        try {
            file.createNewFile();
            System.out.println("new3创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
